//
//  SLLoginViewModel.m
//  StarLight
//
//  Created by Duke.wu on 16/5/11.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLLoginViewModel.h"
#import "SLNetWork.h"
#import "SLUserManager.h"
#import "SLMineViewModel.h"
#import "GeTuiSdk.h"





@implementation SLLoginViewModel

+ (BOOL)checkTelNumber:(NSString *) telNumber{
  NSString *pattern = @"^1+[3578]+\\d{9}";
  NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
  BOOL isMatch = [pred evaluateWithObject:telNumber];
  return isMatch;
}


+ (void)loginWithParameters:(id)parameters{
  
  [SVProgressHUD showWithStatus:@"正在登陆中"];
  NSString *postUrl = [NSString backUrlWithService:@"user" methodName:@"login"];
  
  [SLNetWork postDataWithParameters:parameters postUrl:postUrl complete:^(id respond, NSError *error) {
    
    
    if (!error) {
      NSDictionary *dic = respond;
      if ([dic[@"success"] isEqual:@1]) {
        [SLUserManager storeUserInfoWithObject:respond[@"data"] andKey:loginInfo];
//        [SVProgressHUD showSuccessWithStatus:respond[@"result"]];
        [SLUserManager bindCid:[GeTuiSdk clientId]];
        SLMineViewModel *model = [SLMineViewModel shareInstance];
        
        [model getUserindexCallBack:^(id respond, NSError *error) {
          
          if (!error) {
            [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationToChangeRootVC object:nil];
          } else {
            [SVProgressHUD showErrorWithStatus:@"网络连接出错"];
          }
        }];

      }
      
    }
  }];
  
}



+ (void)sendSMSWithPhoneNumberForRegister:(NSString *)phoneNumber{
  
  NSString *request = [NSString stringWithFormat:@"%@/%@/%@?phone=%@&action=%@",baseUrl,@"user",@"SMSverification",phoneNumber,@"0"];
  
  [SLNetWork getDataWithRequest:request complete:^(id respond, NSError *error) {
    
    if (!error) {
      [SVProgressHUD showSuccessWithStatus:respond[@"result"]];
    }
  }];
  
  
}

+ (void)sendSMSWithPhoneNumberForForget:(NSString *)phoneNumber{
  
  NSString *request = [NSString stringWithFormat:@"%@/%@/%@?phone=%@&action=%@",baseUrl,@"user",@"SMSverification",phoneNumber,@"1"];
  [SLNetWork getDataWithRequest:request complete:^(id respond, NSError *error) {
    [SVProgressHUD showSuccessWithStatus:respond[@"result"]];
  }];
}


+ (void)forgetPwsWithParameters:(id)parameters complete:(LoginVMBlock)complete{
  
  NSString *postUrl = [NSString backUrlWithService:@"user" methodName:@"forgetPws"];
  [SLNetWork postDataWithParameters:parameters postUrl:postUrl complete:^(id respond, NSError *error) {
    if (!error) {
      [SVProgressHUD showSuccessWithStatus:respond[@"result"]];
      complete();
    }
  }];

}

+ (void)testRegisterNumWithParameters:(id)parameters complete:(LoginVMBlock)complete{
  
  NSString *postUrl = [NSString backUrlWithService:@"user" methodName:@"codeVerification"];
  [SLNetWork postDataWithParameters:parameters postUrl:postUrl complete:^(id respond, NSError *error) {
    if (!error) {
      [SVProgressHUD showSuccessWithStatus:respond[@"result"]];
      complete();
    }
  }];
}


+ (void)registerUserWithDataArr:(NSArray *)dataArr complete:(LoginVMBlock)complete{
  NSString *postUrl = [NSString backUrlWithService:@"user" methodName:@"shipdregister"];
  
  NSDictionary *parameters;
  if (dataArr.count > 1) {
    parameters = @{@"userjson":dataArr[0],@"headPhoto":dataArr[1]};
  }else{
    parameters = @{@"userjson":dataArr[0],@"headPhoto":@""};
  }
  
  [SLNetWork uploadFileWithFileArr:dataArr andFileInformationArr:@[@"userjson",@"headPhoto"] andPostUrl:postUrl andParameters:parameters complete:^(id respond, NSError *error) {
    if (!error) {
      [SVProgressHUD showSuccessWithStatus:respond[@"result"]];
      complete();
    }
  }];
}



@end
